﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using NetHelperCore;
using System.Diagnostics;

namespace App_Start
{

    public class CheckApiLoginFilter : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext context)
        {

            try
            {

            }
            catch (Exception ex)
            {
                context.Result = new ContentResult()
                {
                    StatusCode = 500,
                    Content = "请求错误"
                };
                LogHelper.WriteLog(LogHelper.LogType.Error, ex.Message + "---" + ex.StackTrace, "", new StackTrace(new StackFrame(true)).GetFrame(0).GetMethod().Name);
                return;
            }

        }
    }


    /// <summary>
    /// 判断是否为本地请求
    /// </summary>
    public class CheckLocalFilter : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext context)
        {

            try
            {
                if (context.HttpContext.Request.Host.ToString().IndexOf("127.0.0.1") < 0 && context.HttpContext.Request.Host.ToString().ToLower().IndexOf("localhost") < 0)
                {

                    context.Result = new ContentResult()
                    {
                        StatusCode = 500,
                        Content = "未授权的请求"
                    };
                    return;
                }
            }
            catch (Exception ex)
            {
                context.Result = new ContentResult()
                {
                    StatusCode = 500,
                    Content = "请求错误"
                };
                LogHelper.WriteLog(LogHelper.LogType.Error, ex.Message + "---" + ex.StackTrace, "", new StackTrace(new StackFrame(true)).GetFrame(0).GetMethod().Name);
                return;
            }

        }
    }



}
